'use strict';

const Service = require('egg').Service;

class AdminApiService extends Service {
    /**
     * 
     * 添加接口入口界面
     * @param {any} params 
     * @returns 
     * @memberof AdminApiService
     */
    async apiModel(params) {
        let result = await this.app.mysql.query(`
    SELECT
        t.*,
        g.group_name,
        s.system_name,
        s.system_url 
    FROM
        tab_api AS t
        JOIN tab_group AS g ON g.group_id = t.group_id
        JOIN tab_system AS s ON s.system_id = t.system_id 
    WHERE
        t.system_id = '${params.system_id}'
    `);
        let project = await this.app.mysql.get('tab_system', { system_id: params.system_id })
        return {
            result: result,
            project: project
        };

    }
    /**
     * 
     * 查找分类接口分类
     * @param {any} id 
     * @returns 
     * @memberof AdminApiService
     */
    async groupModel(id) {
        let result = await this.app.mysql.query(`
        SELECT * FROM tab_group WHERE system_id='${id}'
        `);
        return result ;
    }

    async showApiModel(id) {
        let result = await this.app.mysql.get('tab_api', { api_id: id });
        return result

    }



    /**
     * 添加接口model
     * 
     * @param {any} params 
     * @returns 
     * @memberof AdminApiService
     */
    async addApiModel(params) {

        let result = await this.app.mysql.insert('tab_api', params)

        return {
            rows: result.affectedRows
        }
    }
    /**
     * 
     * 更新接口model
     * @param {any} parmas 
     * @returns 
     * @memberof AdminApiService
     */
    async updateApiModel(parmas) {
        let sql = `
    UPDATE 
        tab_api 
      SET
        tab_api.api_name='${parmas.api_name}',
        tab_api.api_path='${parmas.api_path}',
        tab_api.api_method='${parmas.api_method}',
        tab_api.group_id='${parmas.group_id}',
        tab_api.api_params='${parmas.api_params}',
        tab_api.api_desc='${parmas.api_desc}'
      WHERE 
        tab_api.api_id='${parmas.api_id}'
        `;
        let result = await this.app.mysql.query(sql);

        return {
            rows: result.affectedRows
        }
    }
    /**
     * 
     * 删除接口
     * @param {any} id  接口id
     * @returns 
     * @memberof AdminApiService
     */
    async deleteApiModel(id) {
        let result = await this.app.mysql.delete('tab_api', { api_id: id })
        return {
            rows: result.affectedRows
        }
    }

   async addGroupModel(params){
        let result = await this.app.mysql.insert('tab_group', params)
        return {
            rows: result.affectedRows
        }
    }
    async deleteGroupModel(id) {
        let result = await this.app.mysql.delete('tab_group', { group_id: id })
        console.log(result,id)
        return {
            rows: result.affectedRows
        }
    }
}

module.exports = AdminApiService;